#!/bin/bash
#kconfig:   2345 90 10
# description:  Redis is a persistent key-value database
#定义变量
#PORT=6379
NAME=redis-server
#CHECK_PORT=`netstat -tnlp|grep "\b$PORT\b"`
REDIS_CONFIG[0]=/opt/tools/redis/redis/redis-6379.conf
REDIS_CONFIG[1]=/opt/tools/redis/redis/redis-6380.conf
REDIS_CONFIG[2]=/opt/tools/redis/redis/redis-6381.conf
REDIS_SENTINEL_CONFIG[0]=/opt/tools/redis/redis/sentinel-26379.conf
REDIS_SENTINEL_CONFIG[1]=/opt/tools/redis/redis/sentinel-26380.conf
REDIS_SENTINEL_CONFIG[2]=/opt/tools/redis/redis/sentinel-26381.conf
REDIS_SERVER=/opt/tools/redis/redis/redis-server
REDIS_SENTINEL_SERVER=/opt/tools/redis/redis/redis-sentinel
RETAVL=0
#检查shelk公共函数库是否存在，存在就加载
FUNCTIONS_PATH=/etc/init.d/functions
[ -f $FUNCTIONS_PATH ]&& source $FUNCTIONS_PATH
#检查redis文件是否存在并可执行
[ -x $REDIS_SERVER ]|| exit 0

#定义函数
#检查是否执行成功
check(){
	RETAVL=$?
	if
		[ $RETAVL -eq 0 ];then
		action "redis is $1" /bin/true
	else
		action "redis is $1" /bin/false
	fi
}
#启动服务
start(){
    #通过遍历下标获取数组元素
  # shellcheck disable=SC2068

  for ((i=0;i<3;i++))
  do
    $REDIS_SERVER ${REDIS_CONFIG[$i]}
    check started
  done
  for ((i=0;i<3;i++))
  do
    $REDIS_SENTINEL_SERVER ${REDIS_SENTINEL_CONFIG[$i]}
    check started
  done

}


#停止服务
stop(){
  ID=`ps -ef | grep "redis" | grep -v "grep" | awk '{print $2}'`
  if [ ! -z "$ID" ];then
      for id in $ID
      do
        kill -9 $id
        check stopped
      done
  else
      action "redis is stop" /bin/true
  fi
}

#重启服务
restart(){
	stop
	sleep 2
	start


}

#判断
case "$1" in
start)
		start
		;;
stop)
		stop
		;;
restart)
		restart
		;;
*)
		echo $"Usage:$0{start|stop|restart|help}"
esac
exit $RETAVL

